package com.revzone.demo001.entity.OtherEntity;

import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class PageResultMap<T> {

    public T map;
    // 分页查询所需的属性
    @ApiModelProperty("页数据条数")
    public Long pageSize = 10L;

    @ApiModelProperty("当前为第几页")
    public Long currentPage = 1L;

    @ApiModelProperty("总页数")
    private Long totalPages;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @ApiModelProperty("总记录数")
    private Long total;
    @ApiModelProperty("总记录数")
    private String type;


    public PageResultMap() {
    }


    public T getMap() {
        return map;
    }

    public void setMap(T map) {
        this.map = map;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Long currentPage) {
        this.currentPage = currentPage;
    }

    public Long getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Long totalPages) {
        this.totalPages = totalPages;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }


    public T convertMapToObject(Class<T> clazz,Map<String, Object> map) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        T targetObject = clazz.newInstance();

        if (map instanceof Map) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();

                Field field = clazz.getDeclaredField(key);
                field.setAccessible(true);
                field.set(targetObject, value);
            }
        }

        return targetObject;
    }
}
